#include "core.h"

CAppManager* CAppManager::getInstance()
{
	static CAppManager *p = NULL;
	if (!p)
	{
		p = new CAppManager;
	}
	return p;
}

CAppManager::CAppManager()
{
	Initialize("");
}

CAppManager::~CAppManager()
{
}

bool CAppManager::Initialize(std::string host)
{
	m_strUrl = "http://";
	m_strUrl += host;
	m_strUrl += "/index";
	return true;
}

bool CAppManager::Destory()
{
	delete this;
	return true;
}

STAppCreateResponse* CAppManager::createApp(STAppCreateRequest *pRequest, std::string method)
{
	std::string requestUrl = m_strUrl;
	requestUrl += "/";
	requestUrl += pRequest->appname;
	m_mapRequestParameter = mapRequestParameter;
	m_vecRequestParameter = vecRequestParameter;
	addQuerryParameter("action", pRequest->action, m_mapRequestParameter, m_vecRequestParameter);
	addQuerryParameter("template", pRequest->strtemplate, m_mapRequestParameter, m_vecRequestParameter);

	std::string strtmp = "";
	if (!Request(requestUrl, method, GetQuerryParameter(m_mapRequestParameter, m_vecRequestParameter, method, strtmp)))
	{
		printf("[%s]:%d\n", __FUNCTION__, __LINE__);
		return NULL;
	}
	
	parseAppCreateResponse();
	return &m_appCreateResponse;
}

bool CAppManager::parseAppCreateResponse()
{
	Json::Reader reader;
	Json::Value result;
	printf("%s\n", g_buf_tmp.data);
	if (reader.parse(g_buf_tmp.data, result))
	{
		m_appCreateResponse.status = result["status"].asString();
		if (m_appCreateResponse.status == "OK")
		{
			m_appCreateResponse.index_name = result["result"]["index_name"].asString();
		}
		else
		{
			uint32_t count = result["errors"].size();
			Json::Value errors = result["errors"];
			for (uint32_t i=0; i<count; i++)
			{
				STErrorInfo errorInfo;
				errorInfo.errorCode = errors[i]["code"].asUInt();
				errorInfo.errorMessage = errors[i]["message"].asString();
				m_appCreateResponse.errorList.push_back(errorInfo);
			}
		}
		m_appCreateResponse.request_id = result["request_id"].asString();
	}
	return true;
}

STAppDeleteResponse* CAppManager::deleteApp(STAppDeleteRequest *pRequest, std::string method)
{
	std::string requestUrl = m_strUrl;
	requestUrl += "/";
	requestUrl += pRequest->appname;
	m_mapRequestParameter = mapRequestParameter;
	m_vecRequestParameter = vecRequestParameter;
	addQuerryParameter("action", pRequest->action, m_mapRequestParameter, m_vecRequestParameter);

	std::string strtmp = "";
	if (!Request(requestUrl, method, GetQuerryParameter(m_mapRequestParameter, m_vecRequestParameter, method, strtmp)))
	{
		printf("[%s]:%d\n", __FUNCTION__, __LINE__);
		return NULL;
	}

	parsedeleteAppResponse();
	return &m_appDeleteResponse;
}

bool CAppManager::parsedeleteAppResponse()
{
	Json::Reader reader;
	Json::Value result;
	printf("%s\n", g_buf_tmp.data);
	if (reader.parse(g_buf_tmp.data, result))
	{
		m_appDeleteResponse.status = result["status"].asString();
		if (m_appDeleteResponse.status == "OK")
		{
			m_appDeleteResponse.result = result["result"].asInt();
		}
		else
		{
			uint32_t count = result["errors"].size();
			Json::Value errors = result["errors"];
			for (uint32_t i=0; i<count; i++)
			{
				STErrorInfo errorInfo;
				errorInfo.errorCode = errors[i]["code"].asUInt();
				errorInfo.errorMessage = errors[i]["message"].asString();
				m_appDeleteResponse.errorList.push_back(errorInfo);
			}
		}
		m_appDeleteResponse.request_id = result["request_id"].asString();
	}
	return true;
}

STAppStatusResponse* CAppManager::statusApp(STAppStatusRequest *pRequest, std::string method)
{
	std::string requestUrl = m_strUrl;
	requestUrl += "/";
	requestUrl += pRequest->appname;
	m_mapRequestParameter = mapRequestParameter;
	m_vecRequestParameter = vecRequestParameter;
	addQuerryParameter("action", pRequest->action, m_mapRequestParameter, m_vecRequestParameter);

	std::string strtmp = "";
	if (!Request(requestUrl, method, GetQuerryParameter(m_mapRequestParameter, m_vecRequestParameter, method, strtmp)))
	{
		printf("[%s]:%d\n", __FUNCTION__, __LINE__);
		return NULL;
	}

	parsestatusAppResponse();
	return &m_appStatusResponse;
}

bool CAppManager::parsestatusAppResponse()
{
	Json::Reader reader;
	Json::Value result;
	printf("%s\n", g_buf_tmp.data);
	if (reader.parse(g_buf_tmp.data, result))
	{
		m_appStatusResponse.status = result["status"].asString();
		if (m_appStatusResponse.status == "OK")
		{
			Json::Value result2 = result["result"];
			m_appStatusResponse.index_name = result2["index_name"].asString();
			m_appStatusResponse.pv = result2["pv"].asInt();
			m_appStatusResponse.doc_last_update_time = result2["doc_last_update_time"].asString();
			m_appStatusResponse.total_doc_num = result2["total_doc_num"].asUInt();
			m_appStatusResponse.instance_out_of_limit = result2["quota"]["instance"]["out_of_limit"].asBool();
			m_appStatusResponse.qps_out_of_limit = result2["quota"]["qps"]["out_of_limit"].asBool();
			Json::Value result3 = result2["fields"]["from_table"]["tables"]["main"]["id"];
			m_appStatusResponse.name = result3["name"].asString();
			m_appStatusResponse.type = result3["type"].asString();
			m_appStatusResponse.is_multi = result3["is_multi"].asString();
			m_appStatusResponse.is_pk = result3["is_pk"].asString();
			m_appStatusResponse.src = result3["src"].asString();
		}
		else
		{
			uint32_t count = result["errors"].size();
			Json::Value errors = result["errors"];
			for (uint32_t i=0; i<count; i++)
			{
				STErrorInfo errorInfo;
				errorInfo.errorCode = errors[i]["code"].asUInt();
				errorInfo.errorMessage = errors[i]["message"].asString();
				m_appStatusResponse.errorList.push_back(errorInfo);
			}
		}
		m_appStatusResponse.request_id = result["request_id"].asString();
	}
	return true;
}

STAppListResponse* CAppManager::listApp(STAppListRequest *pRequest, std::string method)
{
	std::string requestUrl = m_strUrl;
	m_mapRequestParameter = mapRequestParameter;
	m_vecRequestParameter = vecRequestParameter;
	addQuerryParameter("page", pRequest->page, m_mapRequestParameter, m_vecRequestParameter);
	addQuerryParameter("page_size", pRequest->page_size, m_mapRequestParameter, m_vecRequestParameter);

	std::string strtmp = "";
	if (!Request(requestUrl, method, GetQuerryParameter(m_mapRequestParameter, m_vecRequestParameter, method, strtmp)))
	{
		printf("[%s]:%d\n", __FUNCTION__, __LINE__);
		return NULL;
	}

	parselistAppResponse();
	return &m_appListResponse;
}

bool CAppManager::parselistAppResponse()
{
	Json::Reader reader;
	Json::Value result;
	printf("%s\n", g_buf_tmp.data);
	if (reader.parse(g_buf_tmp.data, result))
	{
		m_appListResponse.status = result["status"].asString();
		if (m_appListResponse.status == "OK")
		{
			Json::Value result2 = result["result"];
			int32_t count = result2.size();
			for (int32_t i=0; i<count; i++)
			{
				STAppInfo appInfo;
				appInfo.id = result2[i]["id"].asString();
				appInfo.name = result2[i]["name"].asString();
				appInfo.description = result2[i]["description"].asString();
				appInfo.created = result2[i]["created"].asString();
				m_appListResponse.appLists.push_back(appInfo);
			}
		}
		m_appListResponse.total = result["total"].asString();
		m_appListResponse.request_id = result["request_id"].asString();
	}
	return true;
}